import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/welcome.vue'
import User from '../components/user/Users.vue'
import Rights from '../components/power/rights.vue'
import Roles from '../components/power/roles.vue'
import Cate from '../components/goods/Cate.vue'
import Pramas from '../components/goods/pramas.vue'

Vue.use(Router)

 const router=new Router({
  routes: [{
    // 路由重定向，当用户输入/之后自动调转到登入页面
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
     {
      // 只要进入到home地址，就重定向到welcome
      path: '/home',
      redirect:'/welcome',
      component: Home,
      children: [
        {
          path: '/welcome',
          component:Welcome
        },
        { path: '/users', component: User },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Cate },
        { path: '/params', component: Pramas }
        
      ]
    }
  ]
 })
//  路由导航守卫挂载
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from  代表从哪个路径跳转而来
  // next  是一个函数，表示放行
  // next()---放行     next('/login')强制跳转
  if (to.path === '/login') {
    return next()
  };
  // 如果没有登陆成功获取到token，就定向在登入页面
  const takenStr = window.sessionStorage.getItem('token');
  
  if (!takenStr) {
    return next('/login')
  }
  else next()  
})



export default router